package com.fkdx.webman.controller;

import com.alibaba.fastjson.JSON;
import com.fkdx.dao.entity.PageResult;
import com.fkdx.service.TaskService;
import com.fkdx.service.VoucherService;
import com.fkdx.service.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.fkdx.common.dto.ResponseDTO;
import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户凭证表controller
 */
@Slf4j
@RestController
@RequestMapping("/voucher")
public class VoucherController {

    @Resource
    private VoucherService voucherService;

    @Resource
    private TaskService taskService;

    @GetMapping("/list")
    public PageResult<VoucherQueryResp> getVoucherList(VoucherQueryReq req) {
        return voucherService.getVoucherList(req);
    }

    @GetMapping("/form")
    public VoucherQueryResp getVoucherForm(Long id) {
        return voucherService.getVoucherForm(id);
    }

    @PostMapping("/save")
    public ResponseDTO<String> saveVoucherForm(@RequestBody @Valid VoucherReq req) {
        log.info("接受凭证：{}", JSON.toJSONString(req));
        if(StringUtils.isNotEmpty(req.getVoucher())) {
            voucherService.saveVoucherForm(req);
        } else {
            TaskReq r = new TaskReq();
            r.setId(req.getTid());
            r.setStatus(2);
            taskService.addTaskForm(r);
        }
        return ResponseDTO.buildSuccessDataResp("ok");
    }
    @GetMapping("/test")
    public ResponseDTO<String> test() {
        //voucherService.test();
        return ResponseDTO.buildSuccessDataResp("ok");
    }

    @PostMapping("/remove")
    public ResponseDTO<String> removeVoucherList(@RequestBody Long[] id) {
        voucherService.removeVoucherList(id);
        return ResponseDTO.buildSuccessDataResp("ok");
    }
}
